﻿package com.Zicheng.Action.manage;
import com.Zicheng.BO.CommonBO;
import com.Zicheng.BO.OperateStatus;
import com.Zicheng.BO.${table.TableNameAlias}BO;
import com.Zicheng.Bean.ReturnResult;
import com.Zicheng.DAO.SessionFactory;
import com.Zicheng.DAO.Support.Page;
import com.Zicheng.Entity.${table.TableNameAlias};
import com.Zicheng.Security.Authority;
import com.Zicheng.Security.PermissionConfig;
import com.Zicheng.Security.PermissionItem;
import com.Zicheng.Security.PermissionType;
import com.Zicheng.Utils.GenericUtils;
import com.Zicheng.Utils.RequestUtils;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:${Utils.GetDate} ${Utils.GetTime}
*/ 
public class ${table.TableNameAlias}Action extends ActionSupport {
    Logger logger = Logger.getLogger(${table.TableNameAlias}Action.class);
	private static final String MODULECODE = "${table.TableNameAlias}";
    public ${table.TableNameAlias}Action() {
    }

    /*添加数据*/
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.Add)
    public void Add() {
		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
		#if($c.ColumnType=="Integer")
		 #if(${c.ColumnNameAlias}!=$primaryKeyAlias)
            $c.ColumnType $columnName =RequestUtils.getInteger("${c.ColumnNameAlias}",0);
		  #end
		#elseif($c.ColumnType=="Timestamp")
		  #if((${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
		$c.ColumnType $columnName =Timestamp.valueOf(RequestUtils.getString("${c.ColumnNameAlias}"));
		  #end
		#elseif($c.ColumnType=="Date")
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				$c.ColumnType $columnName = null;
				try {
					$columnName = simpleDateFormat.parse(RequestUtils.getString("${c.ColumnNameAlias}"));
				} catch (ParseException e) {
					e.printStackTrace();  
				}
		#else
		  $c.ColumnType $columnName =RequestUtils.getString("${c.ColumnNameAlias}");
		#end
		#end	
        ${table.TableNameAlias} entity = new ${table.TableNameAlias}();
        Date date = new Date();
        Timestamp ts = new Timestamp(date.getTime());
		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
			#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
			  #if((${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
		         entity.set${c.ColumnNameAlias}($columnName);
			   #else
			     entity.set${c.ColumnNameAlias}(ts);
			   #end
            #end
		#end
        ${table.TableNameAlias}BO bo = new ${table.TableNameAlias}BO();
        bo.save(entity);
		OperateStatus operateStatus = new OperateStatus();
        operateStatus.setMsg("添加成功");
        operateStatus.setStatus(OperateStatus.Status.SUCCESS);        
        ReturnResult result=new ReturnResult();
        result.setDataState(operateStatus);
        GenericUtils.outResult(result);
        
    }
	    /*
    * 删除数据
    * */
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.Delete)
    public void Delete() {
        String ids = RequestUtils.getString("ids");
        CommonBO bo = new CommonBO();
        OperateStatus operateStatus = bo.excuteDelete("${table.TableName}", "${primaryKey}", ids);       
        ReturnResult result=new ReturnResult();
        result.setDataState(operateStatus);
        GenericUtils.outResult(result);
        
    }

	    /*修改数据*/
		 @Authority(moduleCode =MODULECODE,permissionType = PermissionType.Edit)
    public void Edit() {

		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
		#if($c.ColumnType=="Integer")
        $c.ColumnType $columnName =RequestUtils.getInteger("${c.ColumnNameAlias}",0);
		#elseif($c.ColumnType=="Timestamp")
		 #if((${c.ColumnName}!="Update_Date") &&(${c.ColumnName}!="Create_Date"))
		   $c.ColumnType $columnName =Timestamp.valueOf(RequestUtils.getString("${c.ColumnNameAlias}"));
		  #end
		#elseif($c.ColumnType=="Date")
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			$c.ColumnType $columnName = null;
			try {
				$columnName = simpleDateFormat.parse(RequestUtils.getString("${c.ColumnNameAlias}"));
			} catch (ParseException e) {
				e.printStackTrace();  
			}

		#else
		$c.ColumnType $columnName =RequestUtils.getString("${c.ColumnNameAlias}");
		#end
		#end

        ${table.TableNameAlias}BO bo = new ${table.TableNameAlias}BO();
        ${table.TableNameAlias} entity =bo.get(${Utils.ToTitleLower($primaryKeyAlias)});
        Date date = new Date();
        Timestamp ts = new Timestamp(date.getTime());
        #foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
		   #if(${c.ColumnName}!="Create_Date")
			  #if(${c.ColumnName}!="Update_Date")
				 entity.set${c.ColumnNameAlias}($columnName);	
			   #else
					entity.set${c.ColumnNameAlias}(ts);
			   #end
			#end
        #end
        bo.saveOrUpdate(entity);
        OperateStatus operateStatus = new OperateStatus();
        operateStatus.setMsg("修改成功");
        operateStatus.setStatus(OperateStatus.Status.SUCCESS);           
        ReturnResult result=new ReturnResult();
        result.setDataState(operateStatus);
        GenericUtils.outResult(result);
        
    }
    /*数据列表*/
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.List)
    public void List() {
        int pageIndex = RequestUtils.getInteger("page",0);
        int pageSize = RequestUtils.getInteger("rows",0);
		String title = RequestUtils.getString("SearchKey");
        int state = RequestUtils.getInteger("state", -1);
        ${table.TableNameAlias}BO bo = new ${table.TableNameAlias}BO();
        Session session = SessionFactory.getSession();
        Criteria criteria = session.createCriteria(${table.TableNameAlias}.class);
		if (title != "")
            criteria.add(Restrictions.like("enumName", title, MatchMode.ANYWHERE));//修改为适当的查询字段
        if (state>-1)
            criteria.add(Restrictions.eq("status",state));
		criteria.addOrder(Order.desc("sort"));
        criteria.addOrder(Order.desc("${Utils.ToTitleLower(${primaryKeyAlias})}"));
        Page page = bo.pagedQuery(criteria, pageIndex, pageSize);
        List<${table.TableNameAlias}> lst = (List<${table.TableNameAlias}>) page.getResult();
        OperateStatus operateStatus = new OperateStatus();
        operateStatus.setMsg("返回列表成功");
        operateStatus.setStatus(OperateStatus.Status.SUCCESS);        
        ReturnResult result=new ReturnResult();
        result.setTotal(page.getTotalCount());
        result.setDataState(operateStatus);
        result.setRows(lst);
        GenericUtils.outResult(result);
        
    }
	    /*
    * 详细数据信息
    * */
	 @Authority(moduleCode =MODULECODE,permissionType = PermissionType.View)
    public void View() {
        int id = RequestUtils.getInteger("${primaryKeyAlias}", 0);
        ${table.TableNameAlias}BO bo = new ${table.TableNameAlias}BO();
        ${table.TableNameAlias} entity = bo.get(id);
        OperateStatus operateStatus = new OperateStatus();
        operateStatus.setMsg("返回数据详细内容成功！");
        operateStatus.setStatus(OperateStatus.Status.SUCCESS);        
        ReturnResult result=new ReturnResult();
        result.setDataState(operateStatus);
        result.setData(entity);
        GenericUtils.outResult(result);
        
    }
	 /*
    * 更新状态
    * */
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.On)
    public void Audit() {
        String ids = RequestUtils.getString("ids");
        int status = RequestUtils.getInteger("status", -1);
        CommonBO bo = new CommonBO();
        OperateStatus operateStatus = bo.updateStatus("${table.TableName}", "${primaryKey}", "status", status, ids);
        ReturnResult result=new ReturnResult();
        result.setDataState(operateStatus);
        GenericUtils.outResult(result);
        
    }
 /*
    * 当前用户操作权限
    * */
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
    public void Permission() {
        PermissionConfig config = new PermissionConfig();
        try {
            List<PermissionItem> lst = config.getPermissionItems(MODULECODE);
            OperateStatus operateStatus = new OperateStatus();
            operateStatus.setMsg("返回列表成功");
            operateStatus.setStatus(OperateStatus.Status.SUCCESS);
            ReturnResult result = new ReturnResult();
            result.setDataState(operateStatus);
            result.setRows(lst);
            GenericUtils.outResult(result);           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
